package com.xiaomei.manager.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class CodeUtils {
	private static final String DATE_FORMAT_STR = "yyMMddss";
	private static final String USER_CODE_PREFIX = "XM";
	private static Long USER_CODE_NUM = 1l;

	private static final String ORDER_CODE_PREFIX = "NO";
	private static Long ORDER_CODE_NUM = 1l;

	private static final String ACCOUNT_CODE_PREFIX = "ZD";
	private static Long ACCOUNT_CODE_NUM = 1l;
	
	/**
	 * 生成用户编号
	 * @return
	 */
	
	public static synchronized String generateUserCode() {
		String currentTimeStr = getCurrentTimeStr();
		String randomStr = getRandom();
		return USER_CODE_PREFIX.concat("19900").concat(currentTimeStr).concat(randomStr);
		//return USER_CODE_PREFIX.concat("19900").concat(String.valueOf(orderNo));
	}
	
	/**
	 * 生成订单编号
	 * @return
	 */
	public static synchronized String generateOrderCode() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
		String date = sdf.format(new Date());
		
		long orderNo = Long.parseLong(date) * 100000;
		orderNo += ORDER_CODE_NUM;
		ORDER_CODE_NUM ++;
		return ORDER_CODE_PREFIX.concat(String.valueOf(orderNo));
	}
	
	/**
	 * 生成账单编号
	 * @return
	 */
	public static synchronized String generateAccountCode() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
		String date = sdf.format(new Date());
		
		long orderNo = Long.parseLong(date) * 100000;
		orderNo += ACCOUNT_CODE_NUM;
		ACCOUNT_CODE_NUM ++;
		return ACCOUNT_CODE_PREFIX.concat(String.valueOf(orderNo));
	}
	/**
	 * 获取三位随机数
	 * @return
	 */
	public static String getRandom() {
		Random random = new Random();
		String str = String.valueOf(random.nextInt(999));
		if(str.length() == 1) {
			str = "00" + str;
		} else if(str.length() == 2) {
			str = "0" + str;
		}
		return str;
	}
	/**
	 * 获取当前时间字符串
	 * @return
	 */
	public static String getCurrentTimeStr() {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_STR);
		
		return sdf.format(new Date());
	}
	public static void main(String[] args) {
		System.out.println(CodeUtils.generateUserCode());
	}
}
